package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.BuyApplyListAdapter;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.BuyappMainOriginal;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.ExtraValueContent;
import com.isunland.managesystem.entity.rBuyappMain;
import com.isunland.managesystem.utils.MyDateUtil;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

/**
 * 对象功能:  采购申请
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/6/12 18:14
 */
public class BuyApplyListFragment extends BaseListFragment {

  private ArrayList<rBuyappMain> mList;
  private int REQUEST_QUERY = 0;
  private int REQUEST_DETAIL = 1;
  private String beginbuyappDate;
  private String endbuyappDate;
  private String projectId;
  private String projectName;
  private String materialKindCode;
  private String materialKindName;
  private String buyappStatusName;
  private String buyappStatus;
  private String buyPurposeName;
  private String buyPurposeCode;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    setTitleCustom("采购申请列表");
    mList = new ArrayList<>();
    beginbuyappDate = MyDateUtil.format(MyDateUtil.getBeforeMonth(1), MyDateUtil.TYPE_DATE);
    endbuyappDate = MyDateUtil.format(new Date(), MyDateUtil.TYPE_DATE);
    buyPurposeName = "全部";
    buyPurposeCode = "";
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_add_search, menu);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case R.id.menu_item_add:
        Intent intent = new Intent(getActivity(), BuyApplyActivity.class);
        startActivityForResult(intent, REQUEST_DETAIL);
        return true;
      case R.id.menu_item_querys:
        ExtraValueContent queryContent = new ExtraValueContent();
        queryContent.setExtName1(beginbuyappDate);
        queryContent.setExtName2(endbuyappDate);
        queryContent.setExtCode3(projectId);
        queryContent.setExtName3(projectName);
        queryContent.setExtCode4(materialKindCode);
        queryContent.setExtName4(materialKindName);
        queryContent.setExtName5(buyappStatusName);
        queryContent.setExtCode5(buyappStatus);
        queryContent.setExtName6(buyPurposeName);
        queryContent.setExtCode6(buyPurposeCode);
        Intent query = new Intent(getActivity(), BuyApplyQueryActivity.class);
        query.putExtra(BuyApplyQueryFragment.EXTRA_CONTENT, queryContent);
        startActivityForResult(query, REQUEST_QUERY);
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override public String getUrl() {
    return ApiConst.URL_BUY_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    ParamsNotEmpty params = new ParamsNotEmpty();
    params.put("memberCode", CurrentUser.newInstance(getActivity()).getMemberCode());
    params.put("beginbuyappDate", beginbuyappDate);
    params.put("endbuyappDate", endbuyappDate);
    params.put("projectId", projectId);
    params.put("materialKindCode", materialKindCode);
    params.put("materialKindName", materialKindName);
    params.put("buyappStatusName", buyappStatus);
    params.put("buyPurposeCode", buyPurposeCode);
    return params.getParams();
  }

  @Override public void resolveResponse(String json) {
    BuyappMainOriginal original = new Gson().fromJson(json, BuyappMainOriginal.class);
    if (original != null && Base.SUCCESS == original.getResult()) {
      mList.clear();
      mList.addAll(original.getRows());
      BuyApplyListAdapter mAdapter = new BuyApplyListAdapter(getActivity(), mList);
      setListAdapter(mAdapter);
    } else {
      ToastUtil.show("获取列表失败");
    }
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    rBuyappMain content = mList.get(position - 1);
    Intent intent = new Intent(getActivity(), BuyApplyPagerActivity.class);
    intent.putExtra(BuyApplyFragment.EXTRA_CONTENT, content);
    startActivityForResult(intent, REQUEST_DETAIL);
    super.onListItemClick(l, v, position, id);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) return;
    if (requestCode == REQUEST_QUERY) {
      ExtraValueContent queryConten =
          (ExtraValueContent) data.getSerializableExtra(BuyApplyQueryFragment.EXTRA_CONTENT);
      beginbuyappDate = queryConten.getExtName1();
      endbuyappDate = queryConten.getExtName2();
      projectId = queryConten.getExtCode3();
      projectName = queryConten.getExtName3();
      materialKindCode = queryConten.getExtCode4();
      materialKindName = queryConten.getExtName4();
      buyappStatus = queryConten.getExtCode5();
      buyappStatusName = queryConten.getExtName5();
      buyPurposeCode = queryConten.getExtCode6();
      buyPurposeName = queryConten.getExtName6();
    }
    volleyPost();
    super.onActivityResult(requestCode, resultCode, data);
  }
}
